package com.signway.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Pattern;

/**
 * 时间工具类
 */
public class DateTools {
    private final static String TAG = "DateTools";

    /**
     * yyyy-MM-dd
     **/
    public static SimpleDateFormat dateYMD = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * yyyy-MM-dd HH:mm:ss
     **/
    public static SimpleDateFormat dateYMDHMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * yyyy-MM-dd_HH:mm:ss
     **/
    public static SimpleDateFormat date_YMDHMS = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
    public static SimpleDateFormat dateYMD1 = new SimpleDateFormat("yyyyMMdd");
    /**
     * yyyyMMddHHmmss
     **/
    public static SimpleDateFormat dateYMDHMS2 = new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat dateYMDHMS3 = new SimpleDateFormat("yyyyMMddHH:mm:ss");
    /**
     * yyyy-MM-dd_HH:mm:ss
     **/
    public static SimpleDateFormat dateflow = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
    public static SimpleDateFormat dateCamera = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * MM-dd HH:mm:ss
     **/
    public static SimpleDateFormat dateflow2 = new SimpleDateFormat("MM-dd HH:mm:ss");
    /**
     * yyy-MM
     **/
    public static SimpleDateFormat dateym = new SimpleDateFormat("yyyy-MM");
    /**
     * HH:mm
     **/
    public static SimpleDateFormat datehm = new SimpleDateFormat("HH:mm");
    /**
     * HH:mm:ss
     **/
    public static SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
    public static SimpleDateFormat dateHms = new SimpleDateFormat("HHmmss");
    /**
     * yyyyMMddHHmm
     **/
    public static SimpleDateFormat daydf = new SimpleDateFormat("yyyyMMddHHmm");
    /**
     * hh:mm:ss:zzz
     **/
    public static SimpleDateFormat dateHmsS = new SimpleDateFormat("HH:mm:ss.SSS");
    public static SimpleDateFormat yyyyMMddHH = new SimpleDateFormat("yyyyMMddHH");
    public static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");

    public static final String[] weekDay = new String[]{"7", "1", "2", "3", "4", "5", "6"};

    private static Pattern datePattern = Pattern.compile("[0-9]{8}");
    private static Pattern timePattern = Pattern.compile("(([0-1][0-9])|(2[0-3])):[0-5][0-9]");
    private static Pattern time1Pattern = Pattern.compile("[0-9]:[0-5][0-9]");

    /**
     * 修改时区后，重新设置时区。
     *
     * @param timeZone
     */
    public static void setTimeZone(String timeZone) {
        dateYMD.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateYMD1.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateYMDHMS.setTimeZone(TimeZone.getTimeZone(timeZone));
        date_YMDHMS.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateYMDHMS2.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateYMDHMS3.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateflow.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateflow2.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateym.setTimeZone(TimeZone.getTimeZone(timeZone));
        datehm.setTimeZone(TimeZone.getTimeZone(timeZone));
        df.setTimeZone(TimeZone.getTimeZone(timeZone));
        daydf.setTimeZone(TimeZone.getTimeZone(timeZone));
        dateHms.setTimeZone(TimeZone.getTimeZone(timeZone));
    }

    public static boolean isMatchDatePattern(String date) {
        if (!"".equals(date)
                && (date != null)
                && datePattern.matcher(date).matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isMatchTimePattern(String time) {
        if (!"".equals(time)
                && (time != null)
                && timePattern.matcher(time).matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isMatchPosterTimePattern(String time) {
        if (!"".equals(time)
                && (time != null)
                && (timePattern.matcher(time).matches()
                || time1Pattern.matcher(time).matches())) {
            return true;
        } else {
            return false;
        }
    }

    public static String getDateString() {
        Calendar c = Calendar.getInstance();
        return new SimpleDateFormat("yyyyMMdd").format(c.getTime());
    }

    public static String getTimeString() {
        Calendar c = Calendar.getInstance();
        return new SimpleDateFormat("HHmmss").format(c.getTime());
    }

    public static String getHourString() {
        Calendar c = Calendar.getInstance();
        return new SimpleDateFormat("yyyyMMddHH").format(c.getTime());
    }

    /**
     * yyyyMMddHHmmss 转化�?yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String dateChangeFormat(String date) {
        if (date == null || date.length() <= 4) {
            Log.i(TAG, "dateChangeFormat date is " + date + " date length <= 4");
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        String name = date.substring(0, date.length() - 4);
        buffer.append(name.substring(0, 4));// �?
        buffer.append("-");
        buffer.append(name.substring(4, 6));// �?
        buffer.append("-");
        buffer.append(name.substring(6, 8));// �?
        buffer.append(" ");
        buffer.append(name.substring(8, 10));// �?
        buffer.append(":");
        buffer.append(name.substring(10, 12));// �?
        buffer.append(":");
        buffer.append(name.substring(12, 14));// �?
        return buffer.toString();
    }

    /**
     * @param type 0:yy-MM-dd; 1:yyMMdd
     * @return
     */
    public static String getMonthAgoDate(int type) {
        String datemg = "";
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (type == 0) {
            datemg = year + "-" + (month > 9 ? month + "" : "0" + month) + "-"
                    + (day > 9 ? day + "" : "0" + day);
        } else if (type == 1) {

            datemg = year + "" + (month > 9 ? month + "" : "0" + month) + ""
                    + (day > 9 ? day + "" : "0" + day);
        }
        return datemg;
    }

    public static String int2StrTime(int time) {
        String str = null;
        int hour = time / 3600;
        int minute = time % 3600 / 60;
        int second = time % 3600 % 60;
        if (hour < 10) {
            str = "0";
        }
        str += hour;

        str += ":";
        if (minute < 10) {
            str += "0";
        }
        str += minute;

        str += ":";
        if (second < 10) {
            str += "0";
        }
        str += second;

        return str;
    }

//	/**
//	 * time format "00:00:00"
//	 * */
//	public static int strTime2Int(String time) {
//		int result = 0;
//		String[] strBuffer = time.split(":");
//		if (strBuffer == null || strBuffer.length < 3) {
//			return 0;
//		}
//		result = Integer.parseInt(strBuffer[0]) * 3600
//				+ Integer.parseInt(strBuffer[1]) * 60
//				+ Integer.parseInt(strBuffer[2]);
//		return result;
//	}

    /**
     * time format "00:00:00.0" or "00:00:00"
     */
    public static float strTime2Int(String time) {
        float result = 0;
        if (time == null) {
            return 0;
        }

        int index = time.indexOf(".");
        if (index > 0) {
            String[] strBuffer = time.substring(0, index).split(":");
            if (strBuffer == null || strBuffer.length < 3) {
                return 0;
            }
            try {
                result = Integer.parseInt(strBuffer[0]) * 3600
                        + Integer.parseInt(strBuffer[1]) * 60
                        + Integer.parseInt(strBuffer[2])
                        + 0.1f * Integer.parseInt(time.substring(index + 1, time.length()));
            } catch (Exception e) {
                result = 0;
                Log.w(TAG, "strTime2Int error: " + e.toString());
            }

            return result;
        } else {
            String[] strBuffer = time.split(":");
            if (strBuffer == null || strBuffer.length < 3) {
                return 0;
            }
            result = Integer.parseInt(strBuffer[0]) * 3600
                    + Integer.parseInt(strBuffer[1]) * 60
                    + Integer.parseInt(strBuffer[2]);
            return result;
        }
    }

    /**
     * time format "00:00"
     */
    public static int strShortTime2Int(String time) {
        int result = 0;
        String[] strBuffer = time.split(":");
        if (strBuffer == null || strBuffer.length != 2) {
            return 0;
        }
        result = Integer.parseInt(strBuffer[0]) * 3600
                + Integer.parseInt(strBuffer[1]) * 60;
        return result;
    }

    /**
     * time format "00:00:00"
     */
    public static int strLongTime2Int(String time) {
        int result = 0;
        String[] strBuffer = time.split(":");
        if (strBuffer == null || strBuffer.length != 3) {
            return 0;
        }
        result = Integer.parseInt(strBuffer[0]) * 3600
                + Integer.parseInt(strBuffer[1]) * 60
                + Integer.parseInt(strBuffer[2]);
        return result;
    }

    /**
     * 日期转化为long值进行比�?
     *
     * @param _date
     * @return
     */
    public static long Date2long(String _date) {
        SimpleDateFormat sdf = dateYMDHMS;
        Date date = null;
        try {
            date = sdf.parse(_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return 0;
        }
        return (date.getTime());
    }

    /**
     * 日期转化为long值进行比�?
     *
     * @param time
     * @return
     */
    public static String long2Date(long time) {
        SimpleDateFormat sdf = dateYMDHMS;
        String date = null;
        date = sdf.format(time);
        return date;
    }

    // 总毫秒数格式化成时间加毫秒
    public static String long2TimeHmsS(long time) {
        SimpleDateFormat sdf = dateHmsS;
        String formated = null;
        formated = sdf.format(time);
        return formated;
    }

    /**
     * 获取规定格式的日期时�?
     *
     * @param dateFormat
     * @return yyyyMMddHHmmss
     */
    public static String getDate(SimpleDateFormat dateFormat) {
        long currentTimeMillis = System.currentTimeMillis();
        Date currDate = new Date(currentTimeMillis);
        return dateFormat.format(currDate);
    }

    public static String getCameraDate(SimpleDateFormat dateFormat) {
        long currentTimeMillis = System.currentTimeMillis();
        currentTimeMillis -= (8 * 3600 * 1000);
        Date currDate = new Date(currentTimeMillis);
        return dateFormat.format(currDate);
    }

    /**
     * 上刊状态时间
     */
    public static String publishTimeToData(long time) {
        SimpleDateFormat sdf = dateYMDHMS;
        String date = null;
        date = sdf.format(time);
        return date;
    }

    /**
     * 是否是闰年
     */
    public static boolean isLeapYear(int curr_year) {
        if ((curr_year % 400 == 0) || ((curr_year % 4 == 0) && (curr_year % 100 != 0))) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 31天，为大月
     */
    public static boolean isBigMonth(int curr_month) {
        if ((curr_month == 1) || (curr_month == 3) || (curr_month == 5) || (curr_month == 7)
                || (curr_month == 8) || (curr_month == 10) || (curr_month == 12)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 30天，为小月
     */
    public static boolean isSmallMonth(int curr_month) {
        if ((curr_month == 4) || (curr_month == 6) || (curr_month == 9) || (curr_month == 11)) {
            return true;
        } else {
            return false;
        }
    }
}
